package cn.lcsjsxy.yxc.exercise.java050524;

import java.util.HashMap;
import java.util.Map;

/**
 * 题目2：统计单词频率
 * 使用HashMap统计给定字符串数组中每个单词的出现次数。
 * 示例输入：["apple", "banana", "apple", "orange", "banana", "apple"]
 * 输出应显示：apple=3, banana=2, orange=1
 */
public class StatisticsABC {
    public static Map<String, Integer> countWordFrequency(String[] words) {
        Map<String, Integer> frequencyMap = new HashMap<>();
        for (String word : words) {
            frequencyMap.put(word, frequencyMap.getOrDefault(word, 0) + 1);
        }
        return frequencyMap;
    }

    public static void main(String[] args){
        String[] words = {"apple","banana","apple","orange","banana","apple"};
        Map<String,Integer> result = countWordFrequency(words);
        System.out.println(result);
    }
}
